from datetime import datetime
import pandas as pd
import numpy as np

dict = {'name': ["Jack", "Tom", "Helen", "John"], 'age': [28, 39, 34, 36], 'score': [98, 92, 91, 89]}
info = pd.DataFrame(dict)
# 默认随机选择两行
print(info.sample(n=2))
# 随机选择两列
print(info.sample(n=2, axis=1))
print(info.sample(frac=0.5, replace=True))

rng = pd.date_range('1/1/2021', periods=100, freq='D')
# 返回数组
print(len(rng))
ts = pd.Series(np.random.randn(len(rng)), index=rng)
# 降采样后并聚合  只看月分
print(ts.resample('M').mean())

rng = pd.date_range('1/1/2021', periods=10, freq='3D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)

print(ts.resample('D').asfreq().head())
print(ts.resample('D').asfreq().ffill().head())

index = pd.date_range('1/1/2021', periods=6, freq='T')
df = pd.DataFrame({'s': pd.Series([0.0, None, 2.0, 3.0, 4.0, 5.0], index=index)})
print(df)
print(df.asfreq("45s"))

s = pd.Series(["a", "b", "c", "a"], dtype="category")
print(s, '-----1-------')
# 重命名类别
print(["Group %s" % g for g in s.cat.categories])
#  追加类别
print(s.cat.add_categories([5]))
print(s.cat.remove_categories("a"))

print(pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c']), '-----2-------')
print(pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c', 'd'], ['c', 'b', 'a']), '-----3-------')

cat = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c', 'd'], ['c', 'b', 'a'], ordered=True)
print(cat, '-----4-------')
print(cat.min())

cat = pd.Categorical(["a", "c", "c", np.nan], categories=["b", "a", "c"])
df = pd.DataFrame({"cat": cat, "s": ["a", "c", "c", np.nan]})
print(df)
print(df.describe(), '-----5-------')
print(df["cat"].describe(), '-----6-------')
print(cat.categories, '-----7-------')
print(cat.ordered, '-----8-------')

s1 = ['a', 'a', 'b', 'd', 'c']
# 当满足两个类别长度相同时
ss0 = pd.Categorical(s1, categories=['a', 'd', 'b', 'c'])
ss1 = pd.Categorical(s1)
print(ss0==ss1)


s2=['a','b','b','d','c']
#满足上述第二个条件，类别相同，并且ordered均为True
ss0=pd.Categorical(s1,categories=['a','d','b','c'],ordered=True)
ss1 = pd.Categorical(s2,categories=['a','d','b','c'],ordered=True)
print(ss0<ss1)